<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/5
 * Time: 9:55
 */
namespace App\Models;

use OSS\OssClient;
use OSS\Core\OssException;

class Oss{
    static $bucket;
    static function newoss()
    {
        $accessKeyId=env('OSS_ACCESSKEYID');
        $accessKeySecret=env('OSS_ACCESSKEYSECRET');
        $endpoint=env('OSS_ENDPOINT');
        self::$bucket=env('OSS_BUCKET');
        return new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    }

    public static function uploadFile($filePath,$filename)
    {
        if($filename && $filePath)
        {
            $ossClient = self::newoss();
            $ossClient->uploadFile(self::$bucket, $filename, $filePath);
            return true;
        }
        else
        {
            return false;
        }
    }

    public static function delFile($filename)
    {
        if($filename)
        {
            $ossClient = self::newoss();
            $ossClient->deleteObject(self::$bucket, $filename);
            return true;
        }
        else
        {
            return false;
        }
    }

    public static function cutFile($from_object,$to_object)
    {
        if($from_object && $to_object)
        {
            $ossClient = self::newoss();
            $ossClient->copyObject(self::$bucket, $from_object,self::$bucket,$to_object);
            $ossClient->deleteObject(self::$bucket, $from_object);
            return true;
        }
        else
        {
            return false;
        }
    }

    static function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)
            {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar='';
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }
    
    public static function getStsAuth()
    {
        $data['Version'] = '2015-04-01';
        $data['AccessKeyId'] = 'LTAI1OkLUvPVWRFA';
        $data['SignatureMethod'] = 'HMAC-SHA1';
        $data['SignatureVersion'] = '1.0';
        $data['SignatureNonce'] = rand('100000','999999');
        $data['Timestamp'] = date('Y-m-d\TH:i:s\Z',time()-28800);
        //$data['Timestamp'] = date('Y-m-d\TH:i:s\Z',time()-18600);
        $data['Format'] = 'json';
        $data['Action'] = 'AssumeRole';
        $data['RoleArn'] = 'acs:ram::1559363382399075:role/fbwosstemp';
        $data['RoleSessionName'] = 'fbw';
        $data['Signature'] = base64_encode(hash_hmac('sha1','GET&%2F&'.urlencode(self::formatBizQueryParaMap($data, true)),'m2y2Ic96EiSaTBAxEF7cPeLnXqIG9A&',true));
        $bizString = self::formatBizQueryParaMap($data, true);
        $url = 'https://sts.aliyuncs.com?'.$bizString;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($res,true);
        return $res['Credentials'];
    }
}